import {
    ajaxPost,
    showToast
} from '../../../utils/util'
Page({
    /**
     * 页面的初始数据
     */
    data: {
        page: 0,
        limit: 10,
        noGet: false,
        historylist: [],
        inputVal: '',
        listSource: [],
        show: false,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {},

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        if (!wx.getStorageSync('searchList')) {
            this.setData({
                historylist: [],
            })
            return;
        }
        this.setData({
            historylist: wx.getStorageSync('searchList'),
        })
    },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        if (this.data.noGet || !this.data.show) return;
        this.getSearchList();
    },
    searchchangevalue(e) {
        this.setData({
            inputVal: e.detail.value
        })
    },
    search() {
        let inputVal = this.data.inputVal;
        let newHistorylist = this.data.historylist;
        if (!this.data.inputVal) {
            showToast('text', '请输入搜索内容');
            return;
        }
        if (newHistorylist.indexOf(inputVal) == -1) {
            newHistorylist.push(inputVal)
            wx.setStorageSync('searchList', newHistorylist);
        }
        this.setData({
            page: 0,
            noGet: false,
            listSource: [],
            historylist: newHistorylist
        })
        this.getSearchList();
    },
    deleteitem() {
        wx.setStorageSync('searchList', []);
        this.setData({
            historylist: []
        })
    },
    /**
     * 搜索数据获取
     */
    getSearchList() {
        if (this.data.noGet) return;
        let data = {
            token: wx.getStorageSync('token'),
            page: ++this.data.page,
            limit: this.data.limit,
            keywords: this.data.inputVal,
        }
        ajaxPost('/authuser/xxusersearch', data, res => {
            if (res.code == 0) {
                let data = res.data.list;
                if (data.length == 0) {
                    this.setData({
                        show: true,
                        noGet: true
                    })
                } else {
                    if (this.data.limit > data.length) {
                        this.setData({
                            noGet: true
                        })
                    }
                    let newarr = this.data.listSource.concat(data)
                    this.setData({
                        show: true,
                        listSource: newarr,
                    })
                }
            } else {
                showToast('text', res.msg)
            }
        })

    },
    /**
     * 历史搜索记录 点击事件
     * @param {*} e 
     */
    historyClick(e) {
        this.setData({
            inputVal: this.data.historylist[e.target.dataset.index]
        })
        this.setData({
            page: 0,
            noGet: false,
            listSource: [],
        })
        this.getSearchList();
    }

})